#include "StageInfo.h"

// Game ID 0, CTG = Catch The Garbage
// Game ID 1, STD = Save The Dam
// Game ID 2, STM = Suck The Minions
// Game ID 3, SW = Saving Water
// Game ID 4, ER = Escape Rune

StageInfo::StageInfo()
{
	for(int x = 0; x < 5; x++)
		GameNameIndex[x] = 0;

	CurrentStage = 0;
	GameEnd = false;
	StageArranged = true;
	DelayTime = Delay;

	// Random generating game orders
	Order[0] = Randomizer::getInstance().randMax(4,0);

	for(int x = 1; x < 5; x++)
	{
		Order[x] = Order[x-1] + 1;

		if(Order[x] > 4)
		{
			Order[x] = 0;
		}
	}

	// Setting Object info
	Background = Object2D(800.0, 600.0, 0, false);

	for(int x = 0; x < 5; x++)
		GameName[x] = Object2D(150.0, 50.0, 0.0, false);

	// Positioning
	Background.setPosition(400.0, 300.0);
	GameName[Order[0]].setPosition(162.0, 255.0);
	GameName[Order[1]].setPosition(162.0, 495.0);
	GameName[Order[2]].setPosition(401.0, 373.0);
	GameName[Order[3]].setPosition(630.0, 255.0);
	GameName[Order[4]].setPosition(630.0, 495.0);
}

void StageInfo::reset()
{
	// When the game has been restarted, regenerate random stages again
	if(StageArranged == false)
	{
		// Set all the stage name's light off
		for(int x = 0; x < 5; x++)
			GameNameIndex[x] = 0;

		CurrentStage = 0;
		GameEnd = false;
		StageArranged = true;
		DelayTime = Delay;

		// Random generating game orders
		Order[0] = Randomizer::getInstance().randMax(4,0);

		for(int x = 1; x < 5; x++)
		{
			Order[x] = Order[x-1] + 1;

			if(Order[x] > 4)
			{
				Order[x] = 0;
			}
		}
		
		// Positioning
		Background.setPosition(400.0, 300.0);
		GameName[Order[0]].setPosition(162.0, 255.0);
		GameName[Order[1]].setPosition(162.0, 495.0);
		GameName[Order[2]].setPosition(401.0, 373.0);
		GameName[Order[3]].setPosition(630.0, 255.0);
		GameName[Order[4]].setPosition(630.0, 495.0);
	}
}

void StageInfo::freeResource()
{
}

void StageInfo::loadResource()
{
	// Background
	LoadTGA(&TextureBackground, "images/mainmenu/Image_StageInfo.tga", false);

	// Catch The Garbage
	LoadTGA(&CTG[0], "images/mainmenu/CatchTheGarbage_Off.tga", false);
	LoadTGA(&CTG[1], "images/mainmenu/CatchTheGarbage_On.tga", false);

	// Save The Dam
	LoadTGA(&STD[0], "images/mainmenu/SaveTheDam_Off.tga", false);
	LoadTGA(&STD[1], "images/mainmenu/SaveTheDam_On.tga", false);

	// Suck The Minions
	LoadTGA(&STM[0], "images/mainmenu/SuckTheMinions_Off.tga", false);
	LoadTGA(&STM[1], "images/mainmenu/SuckTheMinions_On.tga", false);

	// Saving Water
	LoadTGA(&SW[0], "images/mainmenu/SavingWater_Off.tga", false);
	LoadTGA(&SW[1], "images/mainmenu/SavingWater_On.tga", false);

	// Escape Rune
	LoadTGA(&ER[0], "images/mainmenu/EscapeRune_Off.tga", false);
	LoadTGA(&ER[1], "images/mainmenu/EscapeRune_On.tga", false);

	

}

void StageInfo::draw()
{
	Background.drawPlane(TextureBackground.texID);

	GameName[0].drawPlane(CTG[GameNameIndex[0]].texID);
	GameName[1].drawPlane(STD[GameNameIndex[1]].texID);
	GameName[2].drawPlane(STM[GameNameIndex[2]].texID);
	GameName[3].drawPlane(SW[GameNameIndex[3]].texID);
	GameName[4].drawPlane(ER[GameNameIndex[4]].texID);
}

void StageInfo::update()
{
	// When mini games has not been played till the last, continue update
	if(CurrentStage != 5)
	{
		GameNameIndex[Order[CurrentStage]] = 1;				// Current Stage's name lights up

		// After stage 1 has completed
		if(CurrentStage > 0)
			GameNameIndex[Order[CurrentStage-1]] = 0;		// Previous Stage's name lights off

		// If current stage is Catch The Garbage
		if(Order[CurrentStage] == 0)
		{
			// Update delay time
			float TimePassed = RealTimeController::getInstance().getTimeInterval();
			if(TimePassed > 1000)
				DelayTime -= 1;

			if(DelayTime < 0)
			{
				CurrentStage++;			// Update Stage for later when current game has done
				DelayTime = Delay;		// Set back delay time
				SceneManager::getInstance().changeScene("Catch_Garbage");		// Goes into Catch The Garbage game scene
			}
		}
		
		// If current stage is Save The Dam
		else if(Order[CurrentStage] == 1)
		{
			// Update delay time
			float TimePassed = RealTimeController::getInstance().getTimeInterval();
			if(TimePassed > 1000)
				DelayTime -= 1;

			if(DelayTime < 0)
			{
				CurrentStage++;				// Update Stage for later when current game has done
				DelayTime = Delay;			// Set back delay time
				SceneManager::getInstance().changeScene("Save_Dam");		// Goes into Save The Dam game scene
			}
		}

		// If current stage is Suck The Minions
		else if(Order[CurrentStage] == 2)
		{
			// Update delay time
			float TimePassed = RealTimeController::getInstance().getTimeInterval();
			if(TimePassed > 1000)
				DelayTime -= 1;

			if(DelayTime < 0)
			{
				CurrentStage++;				// Update Stage for later when current game has done
				DelayTime = Delay;			// Set back delay time
				SceneManager::getInstance().changeScene("Suck_Minions");		// Goes into Suck The Minions game scene
			}
		}

		// If current stage is Saving Water
		else if(Order[CurrentStage] == 3)
		{
			// Update delay time
			float TimePassed = RealTimeController::getInstance().getTimeInterval();
			if(TimePassed > 1000)
				DelayTime -= 1;

			if(DelayTime < 0)
			{
				CurrentStage++;				// Update Stage for later when current game has done
				DelayTime = Delay;			// Set back delay time
				SceneManager::getInstance().changeScene("Saving_Water");		// Goes into Saving Water game scene
			}
		}

		// If current stage is Escape Rune
		else if(Order[CurrentStage] == 4)
		{
			// Update delay time
			float TimePassed = RealTimeController::getInstance().getTimeInterval();
			if(TimePassed > 1000)
				DelayTime -= 1;

			if(DelayTime < 0)
			{
				CurrentStage++;				// Update Stage for later when current game has done
				DelayTime = Delay;			// Set back delay time
				SceneManager::getInstance().changeScene("EscapeRune");		// Goes into Escape Rune game scene
			}
		}
	}
	else		// When all mini games has been played
	{
		GameEnd = true;
		StageArranged = false;
		SceneManager::getInstance().changeScene("Main_Menu");			// Back to mainmenu
	}
}

void StageInfo::handleKeyboard(int key, int x, int y)
{
}

void StageInfo::handleKeyboardUp(int key, int x, int y)
{
	// For debug, will delete after game is done
	if(key == 'a')
	{
		CurrentStage++;
		if(CurrentStage>4)
			CurrentStage = 0;
	}
}

void StageInfo::handleKeyboardSpecial(int key, int x, int y)
{
}

void StageInfo::getMid(int &x, int &y)
{
}

void StageInfo::handleMouseClick(int button, int state, int x, int y)
{
}

void StageInfo::handleMouseMove(int x, int y)
{
}